import sys
import random
import webbrowser
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation, QRect, QUrl
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtMultimedia import *


class myPet(QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self)
        self.initUI()

    def initUI(self):
        self.action = 0
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
        self.setAutoFillBackground(False)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.repaint()
        self.img = QLabel(self)
        self.actionDatas = []
        self.initData()
        self.index = 0
        self.setPic('pig1.0.png')
        self.resize(310, 266)
        self.runing = False

        self.timer = QTimer()
        self.timer.timeout.connect(self.actionRun)
        self.timer.start(150)  # 毫秒为单位，动画频率

        self.timeToInit = QTimer()
        self.timeToInit.setSingleShot(True)
        self.timeToInit.timeout.connect(self.initPos)

        self.listView_Anim = QPropertyAnimation(self, b'geometry')
        self.listView_Anim.setDuration(5000)  # 设定动画时间
        self.listView_Anim.setEndValue(QRect(-266, -266, 266, 266))  # 设置终止大小 QRect可以用一组左、顶、宽和高整数来构造

        self.player = QMediaPlayer()
        self.player.setVolume(100)
        self.playlist = QMediaPlaylist()
        self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile('music/clockMusic.mp3')))
        self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile('music/running.mp3')))
        self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
        self.player.setPlaylist(self.playlist)

        self.move(QDesktopWidget().screenGeometry().width() - 300, QDesktopWidget().screenGeometry().height() - 300)

    def getImgs(self, pics):
        listPic = []
        for item in pics:
            img = QImage()
            img.load('img/' + item)
            listPic.append(img)
        return listPic

    def initData(self):
        imgs = self.getImgs(['pig2.0.png', 'pig3.0.png'])
        self.actionDatas.append(imgs)
        imgs = self.getImgs(['pig1.0.png', 'pig4.0.png'])
        self.actionDatas.append(imgs)

    def actionRun(self):
        if not self.runing:
            self.index = 0
            self.runing = True
        self.runFunc(self.actionDatas[self.action])

    def setPic(self, pic):
        img = QImage()
        img.load('img/' + pic)
        self.img.setPixmap(QPixmap.fromImage(img))

    def runFunc(self, imgs):
        if self.index >= len(imgs):
            self.index = 0
            self.runing = False
        self.img.setPixmap(QPixmap.fromImage(imgs[self.index]))
        self.index += 1

    # 初始化玩偶状态
    def initPos(self):
        self.hide()
        screen = QDesktopWidget().screenGeometry()
        self.move(screen.width() - 300, screen.height() - 300)
        self.player.pause()
        self.action = 0
        self.playlist.setCurrentIndex(self.action)

    def clockPlayer(self):
        self.player.play()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = event.globalPos() - self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            self.move(QMouseEvent.globalPos() - self.m_DragPosition)
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag = False
        self.setCursor(QCursor(Qt.ArrowCursor))

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_Escape:
            print('按下了Esc键')
            # self.itsTimeToGo()

    def itsTimeToGo(self):
        if self.action == 0:
            self.action = 1
        self.listView_Anim.start()  # 动画开始
        # 切歌
        self.playlist.setCurrentIndex(self.action)
        self.timeToInit.start(5200)
